package com.njtoyo.taxi.admin.rest.controller.platform;

import com.njtoyo.taxi.admin.aop.PassToken;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.AdminUserResourcePresenter;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.adminUser.AdminUserTokenPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.platform.adminUser.HotCantonWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.platform.auth.AdminUserUpdatePhoneOnlineWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.platform.auth.AdminUserLoginWrapper;
import com.njtoyo.taxi.admin.service.platform.AdminUserService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@RestController
@RequestMapping("/platform/auth")
@Api(tags = "身份验证")
public class AuthController {

    @Autowired
    private AdminUserService adminUserService;

    @PostMapping("/login")
    @ApiOperation("登录")
    @PassToken
    public RestResult<AdminUserTokenPresenter> login(@Valid @RequestBody AdminUserLoginWrapper adminUserLoginWrapper) {
        return adminUserService.adminUserLogin(adminUserLoginWrapper);
    }

    @GetMapping("/self_info")
    @ApiOperation("获取账号信息")
    @SneakyThrows
    public RestResult selfInfo(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser) {
        return adminUserService.getSelfInfo(adminUser);
    }

    @GetMapping("/resource")
    @ApiOperation("获取账号权限")
    @SneakyThrows
    public RestResult<AdminUserResourcePresenter> resource(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser) {
        return adminUserService.getResource(adminUser);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "退出登录", notes = "退出登录，即清除缓存")
    public RestResult logout(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser) {
        return adminUserService.logout(adminUser);
    }

    @PatchMapping("/phone_online")
    @ApiOperation(value = "修改用户电召是否在线")
    public RestResult updatePhoneOnline(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser, @Valid @RequestBody AdminUserUpdatePhoneOnlineWrapper updateWrapper) {
        return adminUserService.updatePhoneOnline(adminUser, updateWrapper);
    }

    @PostMapping("/canton/hot")
    @ApiOperation(value = "热门城市", notes = "新增热门城市，无权限校验")
    public RestResult addHotCanton(@ModelAttribute("admin") AdminUser adminUser, @Valid @RequestBody HotCantonWrapper wrapper) {
        return adminUserService.addHotCanton(adminUser, wrapper);
    }

    @GetMapping("/canton/hot")
    @ApiOperation(value = "获取常用城市", notes = "获取常用城市，无权限校验")
    public RestResult getHotCanton(@ModelAttribute("admin") AdminUser adminUser) {
        return adminUserService.getHotCanton(adminUser);
    }
}
